** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_C5.smcl", replace

use "$project_path/data/4_regdata/dynamics/regdata_dynamics.dta", clear
keep if relative_year==-2 | relative_year==-1

// Generating outcomes
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	gen `v'_m1 = `v' if relative_year==-1
	gen `v'_m2 = `v' if relative_year==-2
}
collapse (mean) runvar *_m1 *_m2, by(Country Year Month Type_Election)
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	gen diff_m2m1_`v' = `v'_m2 - `v'_m1
	drop `v'_m2 `v'_m1
}
foreach v in growth_gdpc_penn inflation_imf unemployment_ilo trade_wb hdi democracy_index {
	egen std_diff_m2m1_`v' = std(diff_m2m1_`v')
}
gen mstd_diff_m2m1_inflation_imf = -std_diff_m2m1_inflation_imf
gen mstd_diff_m2m1_unemployment_ilo = -std_diff_m2m1_unemployment_ilo
egen diff_m2m1_chix_econ = rowmean(std_diff_m2m1_growth_gdpc_penn mstd_diff_m2m1_inflation_imf mstd_diff_m2m1_unemployment_ilo std_diff_m2m1_trade_wb)
egen std_diff_m2m1_chix_econ = std(diff_m2m1_chix_econ)
egen diff_m2m1_general_chix = rowmean(std_diff_m2m1_chix_econ std_diff_m2m1_hdi std_diff_m2m1_democracy_index)

rdrobust diff_m2m1_chix_econ runvar, all
rdrobust std_diff_m2m1_growth_gdpc_penn runvar, all
rdrobust mstd_diff_m2m1_inflation_imf runvar, all
rdrobust mstd_diff_m2m1_unemployment_ilo runvar, all
rdrobust std_diff_m2m1_trade_wb runvar, all
rdrobust std_diff_m2m1_hdi runvar, all
rdrobust std_diff_m2m1_democracy_index runvar, all
rdrobust diff_m2m1_general_chix runvar, all

log close

** Building .tex table

#delimit ;
local outcomes  
		diff_m2m1_chix_econ 
		std_diff_m2m1_growth_gdpc_penn 
		mstd_diff_m2m1_inflation_imf 
		mstd_diff_m2m1_unemployment_ilo
		std_diff_m2m1_trade_wb 
		std_diff_m2m1_hdi 
		std_diff_m2m1_democracy_index 
		diff_m2m1_general_chix 
;
#delimit cr		

local model_count : word count `outcomes'

	// Initialize matrix of results and define value labels
	local ncols = `model_count'*2
	local nrows = 6
	//Prepare matrix of coefficients and other statistics
	mat Coefs = J(`nrows', `ncols', .)
		

//inizialize indices
local i = 0
local pval_col = `model_count'
foreach v in `outcomes'{
	local ++i 
	local ++pval_col
	rdrobust `v' runvar, all
	
	//Store estimates and stats
	local b_`i': di %5.3f e(tau_cl)
	local se_`i': di %5.3f e(se_tau_rb)
	local p_`i': di %5.3f e(pv_rb)
	local N_`i': di %6.0f e(N)
	loca N_eff_`i': di %6.0f e(N_h_l) + e(N_h_r)
	local bw_`i': di %5.1f e(h_r)
	

	
	mat Coefs[1,`i'] = `b_`i''
	mat Coefs[2,`i'] = `se_`i''
	mat Coefs[3,`i'] = `p_`i''	
	mat Coefs[4,`i'] = `N_`i''
	mat Coefs[5,`i'] = `N_eff_`i''
	mat Coefs[6,`i'] = `bw_`i''
	
	mat Coefs[1,`pval_col'] = `p_`i''
	


}
mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
	// Clear the dataset, and use the matrix as dataset			

	clear
	svmat Coefs

	//	input variable names
	input str22 varname 
	"El. turn."
	"SE"
	"p-val"
	"N"
	"N eff."
	"Band."
	
	replace varname = "" if varname == "SE"
	order varname
	
	// Rename cols and labels
	forvalues n = 1/`model_count'{
		rename Coefs`n' Model`n'
	}
	
	//Cols where we store pvalues
	local n_min = `model_count' + 1
	local n_max = `model_count'*2
	local i = 1
	forvalues n = `n_min'/`n_max'{
		rename Coefs`n' P_Model`i'
		local ++i
	}

	label var varname ""
	label var Model1 "\textbf{Econ. perf.}"
	label var Model2 "GDP p.c. gr."
	label var Model3 "(Minus) Inflation"
	label var Model4 "(Minus) Unemp."
	label var Model5 "Trade"
	label var Model6 "\textbf{HDI}"
	label var Model7 "\textbf{Democ.}"
	label var Model8 "\textbf{General index}"
		
	// Convert to string and add options
	forvalues i = 1/`model_count'{
		tostring Model`i', replace force format(%04.3f)
		replace Model`i'= "" if Model`i'=="."
		//Add parenthesis to SE and P-val
		replace Model`i' = "(" + Model`i' + ")" if missing(varname) & Model`i' !=""	
		replace Model`i' = "[" + Model`i' + "]" if varname == "p-val" & Model`i' !=""	
		//Remove decimals to N obs
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N"
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")-1) if varname == "N eff."
		
		replace Model`i' = substr(Model`i', 1, strpos(Model`i', ".")+1) if varname == "Band."

	}
	
	// Add stars
	local 3_star 0.01
	local 2_star 0.05
	local 1_star 0.10
	
	forvalues i = 1/8{
		
		replace Model`i' = Model`i' + "***" if P_Model`i' < `3_star'
		replace Model`i' = Model`i' + "**" if P_Model`i' < `2_star' & P_Model`i' > `3_star'
		replace Model`i' = Model`i' + "*" if P_Model`i' < `1_star' & P_Model`i' > `2_star'
		
		drop P_Model`i'
		
	}

	local title "Placebo tests: Effects of a turnover on the change in outcome variables between two years before the election and the year before the election"
	local label_key "tab:placebo_baseline_diff_m1_year_m2"
	local output "$project_path/output/appendix_tables/Appendix_Table_C5.tex"
	local header "& (1) & (2) & (3) & (4) & (5) & (6) & (7) & (8)"
	local footnote = "\caption*{\footnotesize \emph{Notes}: This table reports estimates for equation (\ref{eq:baseline_rdd}), using as a dependent variable the difference in the level of the outcomes two years before the election and the year before the election.}"

	texsave using "`output'", replace varlabels frag nofix hlines(3) location(h) ///
							  title("`title'")  ///
							  footnote("`footnote'") ///
							  marker("`label_key'") ///
							  headerlines("`header'")
			
